﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.Store.Group_Buy
{
    public partial class Form : FormPage
    {
        BLL.GroupBuy bll = new BLL.GroupBuy();
        Model.GroupBuy gbmodel = new Model.GroupBuy();
        protected EnumHelper.UserType userType;
        protected bool isAdmin;
        Model.Store CurrentStore = new Model.Store();
        public string type = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            userType = Security.GetCurrentUserType();
            isAdmin = userType == EnumHelper.UserType.Administrator;
            CurrentStore = Security.GetCurrentStore();

            //type = Request["type"] == null || Request["type"].ToString() == "" ? "" : Request["type"].ToString();
            //if (type == "history")
            //{
            //    btnSub.Visible = false;
            //}

        }

        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel() { gbmodel = bll.GetModel(Master.id); }

        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            Master.HideSubmit();
        }

        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {

            lbltitle.Text = gbmodel.gbname;

            lblprice.Text = StringPlus.showCoin(gbmodel.gbprice);
            lblorderprice.Text = StringPlus.showCoin(gbmodel.gborderprice);
            lblpayprice.Text = StringPlus.showCoin(gbmodel.gbpayprice);
            lblendtime.Text = gbmodel.gbendtime.AddDays(-2).ToString("yyyy-MM-dd");
            lblendpaytime.Text = gbmodel.gbendtime.ToString("yyyy-MM-dd");
            Label3.Text = (gbmodel.gbendtime.AddDays(-2) - DateTime.Now).Days.ToString()+"天";
            lblcontet.Text = gbmodel.gbcontent;
            rp_Goods_Bing();
           
        }

        protected void rp_Goods_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                TextBox tb_ExpectAmount = (TextBox)e.Item.FindControl("tb_ExpectAmount");

                TextBox tb_Description = (TextBox)e.Item.FindControl("tb_Description");
               
                //if (type == "history")
                //{
                //    tb_ExpectAmount.Enabled = false;
                //    tb_Description.Enabled = false;
                //}

                

                Label lblprice = (Label)e.Item.FindControl("lblprice");

                Label lbltotalPrice = (Label)e.Item.FindControl("lbltotalPrice");
                try
                {
                    if (tb_ExpectAmount.Text == "") tb_ExpectAmount.Text = "0";
                }
                catch 
                {
                    tb_ExpectAmount.Text = "0";
                }
                //lbltotalPrice.Text = StringPlus.showCoin(Convert.ToInt32(tb_ExpectAmount.Text) * Convert.ToDecimal(lblprice.Text));
            }
        }

        void rp_Goods_Bing()
        {
            BBPA.BLL.Group_Good gg_bll = new BLL.Group_Good();
            DataTable dt = gg_bll.GetGroupBuy_GoodList(" gg_gbid=" + Master.id).Tables[0];

            this.rp_Goods.DataSource = dt;
            this.rp_Goods.DataBind();
            Master.ScriptManager_js("Event_Amount_change();");
        }

        protected void btnedit_Click(object sender, EventArgs e)
        {
            //rp_Goods_Bing();
        }

        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {


        }

        protected void btnSub_Click(object sender, EventArgs e)
        {
            BLL.Orders_Goods bllOrders_Goods = new BLL.Orders_Goods();
            BLL.Orders bllOrders = new BLL.Orders();
            Model.Orders modelOrders = new Model.Orders();
            modelOrders.StoreID = CurrentStore.id;
            modelOrders.Number = "tg" + DateTime.Now.ToString("yyMMddHHmm") + CurrentStore.Number;
            modelOrders.State = (int)EnumHelper.OrdersState.OrderSub;
            modelOrders.Gbid = Master.id;
            int OrdersID_new = bllOrders.Add(modelOrders);
            modelOrders = bllOrders.GetModel(OrdersID_new);


            BBPA.BLL.GroupBuy bll_groupbuy = new BLL.GroupBuy();
            BBPA.Model.GroupBuy model_groupbuy = bll_groupbuy.GetModel(Master.id);
          
            foreach (RepeaterItem item in rp_Goods.Items)
            {
                Model.Orders_Goods model = new Model.Orders_Goods();
                int ExpectAmount = DataTypePlus.requestInt(((TextBox)item.FindControl("tb_ExpectAmount")).Text);
                if (ExpectAmount > 0)
                {
                    int tb_id = DataTypePlus.requestInt(((TextBox)item.FindControl("tb_id")).Text);
                    string Description = DataTypePlus.requestString(((TextBox)item.FindControl("tb_Description")).Text);
                    decimal price = DataTypePlus.requestDecimal(((Label)item.FindControl("lblprice")).Text);
                    
                    model.OrdersID = modelOrders.id;
                    model.GoodsID = tb_id;
                    model.ExpectAmount = ExpectAmount;
                    model.Description = Description;

                    model.WaitSupplierCheckNum = ExpectAmount;
                    Master.id = bllOrders_Goods.Add(model);
                    model_groupbuy.gborderprice += (price * ExpectAmount);
                }
            }
           
            if (model_groupbuy.gborderprice > 1)
            {
                bll_groupbuy.Update(model_groupbuy);
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.GroupBuy, Master.id, Master.formState == EnumHelper.FormState.add ? EnumHelper.OperateType.Add : EnumHelper.OperateType.Update, "");
                Master.SubmitOK();
            }
        }
    }
}